package ar.edu.itba.jMEBowling.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

import ar.edu.itba.jMEBowling.Runner;
import ar.edu.itba.jMEBowling.scenario.Scenario;

import com.jme.image.Texture;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.TriMesh;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.physics.contact.MutableContactInfo;
import com.jmex.physics.material.Material;

public class Helpers {

	public static void setTexture(Spatial object, String fileName,
			DisplaySystem displaySystem) {
		Texture t1 = TextureManager.loadTexture(Runner.class.getClassLoader()
				.getResource(fileName), Texture.MinificationFilter.Trilinear,
				Texture.MagnificationFilter.Bilinear);

		TextureState ts1 = displaySystem.getRenderer().createTextureState();
		ts1.setTexture(t1);
		object.setRenderState(ts1);
	}

	public static TriMesh getTriMeshFrom3DS(String modelFileName,
			String nodeName, int pos1, int pos2) {
		Node model = null;
		System.out.println(modelFileName);
		try {
			MaxToJme C1 = new MaxToJme();
			ByteArrayOutputStream BO = new ByteArrayOutputStream();
			URL fileUrl = Scenario.class.getClassLoader().getResource(
					modelFileName);
			System.out.println(fileUrl);
			C1.convert(new BufferedInputStream(fileUrl.openStream()), BO);

			model = (Node) BinaryImporter.getInstance().load(
					new ByteArrayInputStream(BO.toByteArray()));
		} catch (IOException e) {
			System.out.println("Could not load the pinset");
		}

		// List<Spatial> spa = model.getChildren();
		Spatial triMesh = ((Node) model.getChild(pos1)).getChild(pos2);
		return (TriMesh) triMesh;
	}

	public static Node getNodeFrom3DS(String modelFileName, String nodeName,
			final Node rootNode) {
		Node model = null;
		try {
			MaxToJme C1 = new MaxToJme();
			ByteArrayOutputStream BO = new ByteArrayOutputStream();
			URL fileUrl = Scenario.class.getClassLoader().getResource(
					modelFileName);
			C1.convert(new BufferedInputStream(fileUrl.openStream()), BO);

			model = (Node) BinaryImporter.getInstance().load(
					new ByteArrayInputStream(BO.toByteArray()));

		} catch (IOException e) {
			System.out.println("Could not load the pinset");
		}
		return model;
	}

	public static void updateContactInfo(Material material,
			Material contactedMaterial, float sliding, float bounciness) {
		MutableContactInfo contactDetails = new MutableContactInfo();
		contactDetails.setBounce(bounciness);
		contactDetails.setMu(sliding);
		material.putContactHandlingDetails(contactedMaterial, contactDetails);
	}

	public static Material getMaterial(String name, float density) {
		// TODO Auto-generated method stub
		final Material customMaterial = new Material(name);
		customMaterial.setDensity(density);
		return customMaterial;
	}
}
